/*
author:shinion
date : 2019-01-25
功能：
	1.对象初始化基本数据
		控件id，基本初始化数量，初始化工具栏等
		该步初始化只是设置基本的数据信息，此时页面不显示
	2.初始化播放器
		初始化播放器基本信息：控件id，ip信息等
		该步初始化需要显示播放器，并且自动进行命令下发，视频播放等动作


*/

var RealTimeVideoPlayers = {};

RealTimeVideoPlayers.DEF_Max_Picture = 'MaxPicture';
RealTimeVideoPlayers.windowLanguage = "cn.xml";
RealTimeVideoPlayers.webPath = "";
RealTimeVideoPlayers.debug = false;//定义标记，是否允许调试信息输出，输出方法：showDebugInfo();

RealTimeVideoPlayers.channelsData = [];//定义通道列表数据，第一维数据保存通道id，第二维数据保存当前通道开始播放时间
RealTimeVideoPlayers.shplayersTemp = null;//定义多画面播放器
RealTimeVideoPlayers.windowMaxNum = 25;//定义最大画面数
RealTimeVideoPlayers.windowDefaultNum = 4;//定义默认画面数
RealTimeVideoPlayers.heartBeatTime = '30s';//定义发送心跳间隔时间30s
RealTimeVideoPlayers.heartBeatTimerName = 'realTimeVideoHeartBeat';//心跳定时器名称

RealTimeVideoPlayers.playersDiv = "";//定义播放器依赖控件id


RealTimeVideoPlayers.serverIp = '222.187.86.139';//定义流媒体视频服务器ip，并设置默认值
RealTimeVideoPlayers.port = 19350;//定义流媒体视频服务器端口，并设置默认值
RealTimeVideoPlayers.stream = 1;//定义视频码流，默认为1，即子码流


RealTimeVideoPlayers.vehicleId = 0;//定义车辆id
RealTimeVideoPlayers.simNo = '';//定义车辆通讯号
RealTimeVideoPlayers.plateNo = '';//定义车辆车牌号
RealTimeVideoPlayers.channelNum = 0;//定义车辆视频实际通道数，默认为0
RealTimeVideoPlayers.dbVehicleChannelStr = "";//定义保存在数据库的车辆通道保存字符串
RealTimeVideoPlayers.vehicleChannelStr = "";//定义车辆通道保存字符串


RealTimeVideoPlayers.firstFlg = true;//定义第一次标记，默认为true，表示第一次调用该对象
RealTimeVideoPlayers.firstAddChannelFlg = true;//定义第一次添加通道信息标记，默认为true，表明第一次


////////////////////////////////////////////

////////////////////////////////////////////初始化：如：基本数据初始化，播放器初始化

////////////////////////////////////////////


/*
初始化基本数据
	初始化播放器依赖控件id
	初始化流媒体服务器ip、端口
	初始化工具栏
*/
RealTimeVideoPlayers.initBasicData = function(playersDiv,webPath){
	//初始化控件id
	this.playersDiv = playersDiv;
	//初始化地址
	this.webPath = webPath;
	//初始化流媒体服务器ip、端口
	this.initTools();
}



/**
 * 初始化播放器
 * 	1.首先判断控件id不为空
 * 	2.获取simNo等信息，主要是simNo长度大于等于11，如果为11位，则需要补足12位，车辆视频通道数量要大于0
 * 
 * 	3.如果当前第一次初始化标记为false，则需要先发送停止命令，然后再重新初始化信息
 */
RealTimeVideoPlayers.initShplayers = function(vehicleTemp,stopFlg){
	var me = this;
	me.showDebugInfo("开始初始化对象");
	if (vehicleTemp) {

		//多次初始化之前并且允许停止需要停止之前的请求
		if(!me.firstFlg && stopFlg){
			me.doStopAll();
		}

		me.channelNum = vehicleTemp.videoChannelNum;
		me.vehicleId = vehicleTemp.vehicleId;

		//校验simNo
		var simNoTemp = ''+vehicleTemp.simNo;
		if(simNoTemp.length <= 11)
			simNoTemp = '0'+simNoTemp;
		vehicleTemp.simNo = simNoTemp;


		var obj = {
			id: me.playersDiv,
			serverIp: me.serverIp,
			port: me.port,
			simNo: vehicleTemp.simNo,
			plateNo: vehicleTemp.plateNo,
			playerNum:me.windowDefaultNum,
			firstFlg:me.firstFlg,
		};
		// me.showDebugInfo("创建多画面播放器开始：");
		// me.showDebugInfo(obj);//输出参数，比对


		

		if (me.shplayersTemp == null) {
			me.shplayersTemp = new shplayers(obj);
			me.firstFlg = false;
		} else {
			me.shplayersTemp.init(obj);
		}
		// me.showDebugInfo(me.shplayersTemp);//输出对象，比对结果
		//初始化完成后不自动发送请求
		// me.sendAllRealVideoRequest();
	}
}

/*
初始化播放器
	1.校验数据，判断数据时候合法，如果数据不合法，直接退出
	2.判断车辆是否改变(即simNo是否不同)，如果相同，则无需初始化，直接退出
		如果不同，则先停止当前通道列表中的所有视频，然后再进行更改操作
	3.获取通道信息数据，并初始化到通道列表中
	4.判断shplayersTemp是否存在，
		如果不存在，即第一次更新，需要创建多画面播放器对象shplayersTemp，
		如果存在，则判断播放器画面数是否足够播放，如果不够，需要添加，如果够，则不操作
	5.循环调用shplayersTemp.initChannel(obj)方法初始化单个播放器信息
		剩余不需要播放视频的播放器则初始化为默认模式

*/
RealTimeVideoPlayers.initShplayers2 = function(vehicleTemp,channelMsg){
	var me = this;

	me.showDebugInfo("RealTimeVideoPlayers.initShplayers2-->开始初始化");

	//校验
	if(me.checkVehicleData(vehicleTemp)){
		me.showDebugInfo("RealTimeVideoPlayers.initShplayers2-->校验成功");
		me.showDebugInfo(vehicleTemp);
	}else{
		// return;
	}
	//判断车辆是否改变
	me.showDebugInfo("当前车辆通讯号："+me.simNo);


	//如果通道数据改变，询问修改
	// me.getChannelsDataChange();

	//停止通道列表中所有通道视频
	// me.doStopAll();

	//更新车辆信息
	me.showDebugInfo("开始更新车辆信息");

	me.simNo = vehicleTemp.simNo;
	me.plateNo = vehicleTemp.plateNo;
	me.vehicleId = vehicleTemp.vehicleId;
	me.channelNum = vehicleTemp.videoChannelNum;
	// me.vehicleChannelStr = vehicleTemp.vehicleChannelStr;//赋值通道数据保存字符串
	// me.dbVehicleChannelStr = vehicleTemp.vehicleChannelStr;//赋值数据库中的通道数据保存字符串
	

	//初始化shplayersTemp(多画面播放器对象)
	me.initShplayersTemp();

	//获取数据库中保存的通道数据字符串
	me.getAjaxChannelStr(channelMsg);


	//获取通道列表数据
	// me.getChannelsData();
	


	//初始化通道列表
	// me.initChannels();


}




/*
校验车辆数据
	校验车辆数据是否合法
	校验simNo并进行补全
*/
RealTimeVideoPlayers.checkVehicleData = function(vehicleTemp){
	var me = this;
	var returnFlg = false;
	try {
		// me.showDebugInfo("校验前的数据为 ：");
		// me.showDebugInfo(vehicleTemp);

		var simNoTemp = ''+vehicleTemp.simNo;
		//校验数据
		if(me.isUndefined(simNoTemp)){
			return returnFlg;
		}
		if(me.isUndefined(vehicleTemp.plateNo)){
			return returnFlg;
		}
		if(me.isUndefined(vehicleTemp.vehicleId)){
			return returnFlg;
		}
		if(isNaN(parseInt(vehicleTemp.videoChannelNum))){
			return returnFlg;
		}
		//判断并格式化simNo
		if(simNoTemp.length <= 11)
			simNoTemp = '0'+simNoTemp;
		vehicleTemp.simNo = simNoTemp;

		//判断车辆通道保存字符串是否合法
		if(me.isUndefined(vehicleTemp.vehicleChannelStr)){
			// vehicleTemp.vehicleChannelStr = "";
		}

		returnFlg = true;
	} catch (error) {
		this.showErrorInfo("RealTimeVideoPlayers-->checkVehicleData-->error："+error);
	}
	return returnFlg;
}


//初始化多画面播放器信息
RealTimeVideoPlayers.initShplayersTemp = function(){
	var me = this;
	try {
		var obj = {
			id: me.playersDiv,
			serverIp: me.serverIp,
			port: me.port,
			simNo: me.simNo,
			plateNo: me.plateNo,
			playerNum:me.windowDefaultNum,
			firstFlg:me.firstFlg,
		};
		me.showDebugInfo("创建多画面播放器开始：");
		me.showDebugInfo(obj);//输出参数，比对


		

		if (me.shplayersTemp == null) {
			me.shplayersTemp = new shplayers(obj);
			me.firstFlg = false;
		} else {
			me.shplayersTemp.init(obj);
		}
	} catch (error) {
		me.showErrorInfo("初始化多画面播放器错误："+error);
	}
}

/*
获取通道数据
 */
RealTimeVideoPlayers.getChannelsData = function(){
	var me = this;

	this.channelsData = [];//初始化通道列表为空
	me.showDebugInfo("获取通道数据保存字符串数据为 ： " + me.vehicleChannelStr);
	if(me.vehicleChannelStr == ""){
		//通道保存数据为空，则获取所有通道信息
		var str = "";
		for(var i=0 ; i<me.channelNum ; i++){
			me.channelsData[i] = [];
			me.channelsData[i][0] = i+1;
			me.channelsData[i][1] = 0;
			if(i == 0){
				str += (i+1);
			}else{
				str += "," + (i+1);
			}
		}
		//播放默认设置更新为全选
		me.vehicleChannelStr = str;
	}else{
		try {
			var channelsTemp = me.vehicleChannelStr.split(",");
			if(!me.isUndefined(channelsTemp)){
				if(channelsTemp.length > 0){
					$.each(channelsTemp,function(n,channel){
						var channelTemp = parseInt(channel);
						if(!isNaN(channelTemp) && channelTemp > 0){
							var temp = [];
							temp[0] = channelTemp;
							temp[1] = 0;
							me.channelsData.push(temp);
							// me.channelsData[n] = [];
							// me.channelsData[n][0] = channelTemp;
							// me.channelsData[n][1] = 0;
						}
					});
				}
			}
			me.showDebugInfo(me.channelsData);
		} catch (error) {
			me.showErrorInfo("获取车辆通道数据保存信息错误 :"+error);
		}
	}
}

//初始化通道列表数据
RealTimeVideoPlayers.initChannels = function(){
	var me = this;
	this.showDebugInfo("通道列表数量 = " + this.channelsData.length);
	$.each(me.channelsData,function(n,channelTemp){
		me.showDebugInfo("第"+ (n+1) +"个画面的通道号为："+channelTemp[0]);
		if(me.shplayersTemp != null && channelTemp[0] > 0){
			var channelId = channelTemp[0];
			var channelMsg = me.plateNo + "  通道" + channelTemp[0];
			var url = "rtmp://"+me.serverIp+":"+me.port+"/live/"+me.simNo+"_"+ channelId;
			me.shplayersTemp.setOnePlayerUrl((n+1),url);
			me.shplayersTemp.showOnePlayerChannelMsg((n+1),channelMsg,true);
		}
	});
	var start = me.channelsData.length;
	var end = me.shplayersTemp.playerNum;
	if(start < end){
		//设置其他空闲画面的通道信息
		for(var i=start ; i<end ; i++){
			me.shplayersTemp.showOnePlayerChannelMsg((i+1),"",true);
			me.shplayersTemp.showOnePlayerCmdMsg((i+1),"");
		}
	}
}


//获取数据库中保存的通道数据字符串
RealTimeVideoPlayers.getAjaxChannelStr = function(channelmsg){
	var me = this;
	me.showDebugInfo("获取数据库中的通道数据字符串");


	var url = me.webPath + "/vehicleRealTimeVideo/getChannelDataStr.action";
	var param = {
		vehicleId:me.vehicleId,
	};
	this.showDebugInfo("请求数据地址为 ："+ url + " , 参数为 ： ");
	me.showDebugInfo(param);

	Utility.getAjaxData(url,JSON.stringify(param),function(data){
		me.showDebugInfo("获取到数据库中的通道信息数据  ： ");
		me.showDebugInfo(data);
		if(data.success){
			//查询成功
			if(data.data.length >0){
				me.vehicleChannelStr = data.data[0].channelsDataStr;
				me.dbVehicleChannelStr = me.vehicleChannelStr;
			}else{
				//没有保存数据
				me.vehicleChannelStr = "";//赋值通道数据保存字符串
				me.dbVehicleChannelStr = "";//赋值数据库中的通道数据保存字符串
			}
			if(channelmsg != ""){
				//只显示新通道
				// me.editChannelsData(channelmsg);//编辑通道数据
				//在原有基础上添加新通道，并且只播放新的视频通道，其他通道不操作
				me.editChannelsData(me.vehicleChannelStr);
				// me.addOneChannelData(parseInt(channelmsg));
			}else{
				me.editChannelsData(me.vehicleChannelStr);//编辑通道数据
				me.sendAllRealVideoRequest();//发送视频请求
			}
		}else{
			//查询失败
		}
	});
}





//初始化工具栏
RealTimeVideoPlayers.initTools = function () {
	var me = this;
	//是否隐藏工具栏
	var hideTool = getUrlParameter("hideTool");
	me.showDebugInfo("hideTool");
	me.showDebugInfo(hideTool);
	if (hideTool == null || !hideTool) {
		$('.tv_box').show();
	}
	var showTitle = getUrlParameter("showTitle");
	if (showTitle != null && showTitle) {
		$('.tv_play').show();
		$('.tv_play .tv_play_span').show();
	}
	var changeSize = getUrlParameter("changeSize");
	if (changeSize != null && changeSize) {
		$('.tv_play').show();
		$('.tv_play .btn_tv_icon').show();
	}

	$("#box_top").hide();
	$("#box_bottom").hide();
	$(".icon_num1").click(function () {
		me.doSwitchWindow(1);
		console.log("1 size");
	});
	$(".icon_num2").click(function () {
		me.doSwitchWindow(4);
		console.log("4 size");
	});
	$(".icon_num3").click(function () {
		me.doSwitchWindow(6);
		console.log("6 size");
	});
	$(".icon_num4").click(function () {
		me.doSwitchWindow(8);
		console.log("8 size");
	});
	$(".icon_num5").click(function () {
		me.doSwitchWindow(9);
		console.log("9 size");
	});
	$(".icon_num6").click(function () {
		me.doSwitchWindow(16);
		console.log("16 size");
	});
	var picture = $.cookie(me.DEF_Max_Picture);
	if (picture && picture == 1) {
		windowMaxNum = 25;
		$(".icon_num7").click(function () {
			console.log("25 size");
			// doSwitchWindow(25);
		});
		$(".icon_num8").parent().hide();
	} else if (picture && picture == 2) {
		windowMaxNum = 36;
		$(".icon_num7").click(function () {
			console.log("25 size");
			// doSwitchWindow(25);
		});
		$(".icon_num8").click(function () {
			console.log("36 sieze");
			// doSwitchWindow(36);
		});
	} else {
		windowMaxNum = 16;
		$(".icon_num7").parent().hide();
		$(".icon_num8").parent().hide();
	}
	$(".icon_num9").click(function () {
		InfoWindow.editUserPreference();

	});
//视频显示比例
// $("#box_viewSize").hover(
// 	function () {
// 		$(this).find("ul").show();
// 	},
// 	function () {
// 		$(this).find("ul").hide();
// 	});
// $("#view4X3").click(function () {
// 	console.log("4:3");
// 	// doViewSize("4:3");
// });
// $("#view16X9").click(function () {
// 	console.log("16:9");
// 	// doViewSize("16:9");
// });
// $("#viewFull").click(function () {
// 	console.log("funn screen");
// 	// doViewSize("full");
// });
//视频显示比例
// $("#box_viewMode").hover(
// 	function () {
// 		$(this).find("ul").show();
// 	},
// 	function () {
// 		$(this).find("ul").hide();
// 	});
// $("#bufferShort").click(function () {
// 	console.log("buffershort");
// 	// doBufferTime(fastBufferTime);
// });
// $("#bufferLong").click(function () {
// 	console.log("bufferlong");
// 	// doBufferTime(smoothBufferTime);
// });
$("#box_capture").hide();
//$("#box_voice").hide();

$(".icon_play_all").click(function () {
	me.doPlayAll();
});
$("#stopAll").click(function () {
	$("#box_stop").find("ul").hide();
	// $("#box_stop").find("ul").hide();
	me.doStopAll();
});
$("#clearAll").click(function () {
	// console.log("clear all")
	$("#box_stop").find("ul").hide();
	me.doClearAll();
});

//停止视频
$("#box_stop").hover(
	function () {
		$(this).find("ul").show();
	},
	function () {
		$(this).find("ul").hide();
	});

/*
声音点击
*/
$("#box_voice").click(function () {
	var cssTemp = $("#volumeSlider").css("display");
	me.showDebugInfo(cssTemp);
	if (cssTemp == "block") {
		$("#volumeSlider").css("display", "none");
	} else if (cssTemp == "none") {
		$("#volumeSlider").css("display", "block");
	}
});

$(".btn_tv_icon").click(function () {
	// if( $(this).hasClass("btn_tv_min") ){
	// 	$(this).removeClass("btn_tv_min").addClass("btn_tv_max");
	// 	if(typeof parent.doClickHideMap == 'function') {
	// 		parent.doClickHideMap(true);
	// 	}
	// }else{
	// 	$(this).removeClass("btn_tv_max").addClass("btn_tv_min");
	// 	if(typeof parent.doClickHideMap == 'function') {
	// 		parent.doClickHideMap(false);
	// 	}
	// }
	console.log("btn_tv_icon.click");
});
}




////////////////////////////////////////////

////////////////////////////////////////////功能方法：如：播放视频，发送心跳等

////////////////////////////////////////////


/*
发送所有实时视频请求
	根据通道列表数组中的数据，发送视频命令
*/
RealTimeVideoPlayers.sendAllRealVideoRequest = function(){
	var me = this;
	try {
		if(me.isUndefined(me.channelsData) || me.channelsData.length <= 0){
			return ;
		}
		//如果当前播放器的画面数小于0或者获取错误，则直接退出
		var playersNum = 0;
		if(me.shplayersTemp != null){
			playersNum = parseInt( me.shplayersTemp.playerNum);
		}
		if(isNaN(playersNum) || playersNum <= 0){
			return ;
		}
		//遍历通道列表数据，然年发送命令
		me.showDebugInfo(me.channelsData);
		me.showDebugInfo("当前请求的simNo为："+ me.simNo+" , 车牌号为 ："+me.plateNo + " , 当前画面数为 ： "+playersNum);
		$.each(me.channelsData,function(n,channelTemp){
			if(n < playersNum){//当播放器索引小于播放器画面数时
				if(!me.isUndefined(channelTemp)){
					if(!isNaN(channelTemp[0]) && channelTemp[0] > 0){
						me.sendRealVideoRequest(channelTemp[0]);
						me.showDebugInfo("当前请求的开始视频命令通道号为："+ channelTemp[0]);
						var now = new Date();
						channelTemp[1] = now.getTime();
					}
				}
			}
		});
		me.showDebugInfo(me.channelsData);
	} catch (error) {
		me.showErrorInfo("发送所有视频请求错误 : "+error);
	}
	
}


/**
 * 发送实时视频请求
 */
RealTimeVideoPlayers.sendRealVideoRequest = function(channel)
{
	var me = this;
	var params = {
		vehicleId:me.vehicleId,
		simNo : me.shplayersTemp.simNo, 
		channel:channel, 
		stream:me.stream,
	};
    var url = globalConfig.webPath + "/videoCommand/realTimeVideo.action";
	$.getJSON(url, params, 
			function(result){
		if (result.success) {
			var commandId = result.data; //下发成功后，获取到命令Id
			me.startQueryResult(commandId,channel);
		}else
		{
			me.showDebugInfo("实时视频请求命令错误 : "+result);
		}

	});
}

/*
查询执行结果
	通过定时器，每秒查询一次，如果一直查询不成功，则提示失败信息
	如果查询成功，则播放器开始播放
*/
RealTimeVideoPlayers.startQueryResult = function(commandId,channel){
	var me = this;
	var url = globalConfig.webPath + "/command/getCommandResult.action";
	var params = {commandId:commandId};
	var timername = "queryCommandResult"+commandId;//定义定时器名称
	var time = 0;
	$(document).everyTime("500ms",timername,function(){
		// console.log("查询命令执行结果");
		time ++;
		$.getJSON(url, params,function(result){
			// console.log(result);
			if(result.success){
				$(document).stopTime(timername);
				me.doPlayOne(channel);
				me.showDebugInfo("实时视频命令执行成功");
			}else{
				// console.log("命令暂时未执行成功");
			}
			me.showOneChannelCmdMsg(channel,result.message);
			
		});
		if(time >=10){
			$(document).stopTime(timername);
			me.showDebugInfo("实时视频命令查询超过10次，停止查询");
			me.showOneChannelCmdMsg(channel,"请求失败");
		}
	});
}

/*
在指定通道上显示命令结果信息
*/
RealTimeVideoPlayers.showOneChannelCmdMsg = function(channel,msg){
	var me = this;
	try {
		if(me.shplayersTemp != null){
			// me.showDebugInfo("当前通道号为："+ channel);
			var playerIndex = me.getPlayerIndex(channel) + 1 ;
			if(!isNaN(playerIndex) && playerIndex > 0){
				me.shplayersTemp.showOnePlayerCmdMsg(playerIndex,msg);
			}
		}
	} catch (error) {
		me.showErrorInfo("显示通道"+channel+"命令信息错误 : "+error);
	}
	
}


/*
播放所有视频
*/
RealTimeVideoPlayers.doPlayAll = function(){
	this.showDebugInfo("play all players");
	if(this.shplayersTemp != null){
		this.sendAllRealVideoRequest();
	}
}


/*
播放单个视频
*/
RealTimeVideoPlayers.doPlayOne = function(channel){

	this.showDebugInfo("播放通道号："+channel);
	if(this.shplayersTemp != null){
		// this.shplayersTemp.playOneVideo(channel);
		var playerIndex = this.getPlayerIndex(channel) +1;
		if(!isNaN(playerIndex) && playerIndex > 0){
			this.showDebugInfo("通道号为：" + channel + " , 在多画面播放器中的索引为 : "+playerIndex);
			this.shplayersTemp.playOneVideo(playerIndex);
			// var now = new Date();
			// this.channelsData[playerIndex-1][1] = now.getTime();
		}
		// console.log(" new simNo = "+shplayersTemp.simNo);
		// console.log("视频地址："+shplayersTemp.getPlayerByChannel(channel).getUrl());
	}
}



/*
停止所有的视频
*/
RealTimeVideoPlayers.doStopAll = function(){
	var me = this;
	me.showDebugInfo("stop all players");
	if(me.shplayersTemp != null){
		me.shplayersTemp.stopAllVideos();
		
		//停止通道列表中的视频
		if(me.channelsData.length > 0){
			$.each(me.channelsData,function(n,channel){
				var channelTemp = parseInt(channel[0]);
				if(!isNaN(channelTemp) && channelTemp > 0){
					me.showDebugInfo("下发停止命令,通道："+ channelTemp);
					me.sendStopVideoRequest(channelTemp);
					channel[1] = 0;//设置开始时间为0
				}
			});
		}
		me.showDebugInfo(me.channelsData);
	}	
}


/**
 * 发送停止实时视频请求
 */
RealTimeVideoPlayers.sendStopVideoRequest = function(channel){
	var me = this;
	var params = {
		// vehicleId:realTimeVideoBasicData.vehicleId,
		simNo : me.shplayersTemp.simNo,
		channelId:channel, 
		// stream:realTimeVideoBasicData.stream,
	};
    var url = globalConfig.webPath + "/videoCommand/stopVideoPlay.action";
	$.getJSON(url, params, function(result){
		// console.log(result);
		if (result.success) {
			me.showDebugInfo("通道"+channel+"停止视频命令请求成功 :"+result.message);
			
		}else
		{
			me.showDebugInfo("通道"+channel+"停止视频命令请求成功 :"+result.message);
		}

	});
}



/*
清除所有视频播放请求
*/
RealTimeVideoPlayers.doClearAll = function(){
	this.showDebugInfo("clear all");
	//清除播放请求
	this.clearRequest();
	$(document).stopTime(this.heartBeatTimerName);
	this.formatShplayers();
}

/*
格式化视频，即重置到初始状态，但是不请求视频
*/
RealTimeVideoPlayers.formatShplayers = function(){
	var me = this;
	me.showDebugInfo("开始重置播放器");
	if(me.shplayersTemp != null){
		var obj = {
			id: me.playersDiv,
			serverIp: me.serverIp,
			port: me.port,
			simNo: me.shplayersTemp.simNo,
			plateNo: me.shplayersTemp.plateNo,
			playerNum:me.windowDefaultNum,
			firstFlg:me.firstFlg,
		};

		me.showDebugInfo("清空多画面播放器");
		$("#"+me.playersDiv).html("");
		me.shplayersTemp = null;

		me.showDebugInfo("新建多画面播放器");
		me.shplayersTemp = new shplayers(obj);
	}
	
}


/*
清除播放请求
 */
RealTimeVideoPlayers.clearRequest = function() {
    //如果刷新了页面，则此前所有的请求，都清掉
    var url = globalConfig.webPath + "/videoRequest/clear.action";
    var param = {};
    $.getJSON(url, param, function(result)
    {
        if(console)
            console.log("清除以前的播放请求:" + (result.success ? "成功" : "失败"));
    });
}


/*
根据通道id，获取该通道在多画面播放器中的索引
*/
RealTimeVideoPlayers.getPlayerIndex = function(channelId){
	var me = this;
	var returnNum = -1;
	var flg = true;
	if(this.channelsData.length > 0){
		$.each(me.channelsData,function(n,channelTemp){
			if(!me.isUndefined(channelTemp)){
				if(channelTemp[0] == channelId){
					if(flg){
						returnNum = n;
						flg = false;
					}
				}
			}
		});
		
	}
	// me.showDebugInfo("获取到当前通道id数据，索引号为：" + returnNum);
	return returnNum;
}



/*
选择画面数
*/
RealTimeVideoPlayers.doSwitchWindow = function(num){
	this.showDebugInfo("doSwithWindow num = " + num);
	this.windowDefaultNum = num;
	if(this.shplayersTemp != null){
		this.shplayersTemp.init({playerNum:num});
	}
}



//发送心跳
RealTimeVideoPlayers.sendHeartBeat = function(){
	var me = this;
	me.showDebugInfo("发送心跳");
	var url = globalConfig.webPath+"/videoRequest/heartBeat.action";
	var param = {};
	$.getJSON(url, param, function (result) {
		me.showDebugInfo("heartBeatResult");
		me.showDebugInfo(result);
	});
}


//修改通道列表保存数据
RealTimeVideoPlayers.editChannelsData = function(channelsStr){
	this.showDebugInfo("开始更新通道数据----> 新通道信息为 ："+channelsStr);
	//停止所有视频请求
	this.doStopAll();
	//修改保存数据
	this.vehicleChannelStr = channelsStr;
	//初始化多画面播放器
	this.initShplayersTemp();
	//获取通道数据
	this.getChannelsData();
	//初始化通道信息
	this.initChannels();
}

//添加一条通道视频
/*
添加通道
	1.校验数据，判断数据是否合法，如果不合法则不操作，直接退出
	2.判断车辆是否改变（即通讯号是否不同），如果改变，则调用initshplayers2方法，如果没改变，不初始化
	3.添加新通道

*/
RealTimeVideoPlayers.addOneChannelData = function(vehicleTemp,channel){
	var me = this;
	me.showDebugInfo("开始添加通道" + channel + "视频");

	//校验数据
	if(!me.checkVehicleData(vehicleTemp)){
		me.showDebugInfo("添加通道-->校验数据失败");
		return;
	}

	//判断车辆是否改变,不同则初始化，相同则不操作
	if(vehicleTemp.simNo != me.simNo){
		me.initShplayers2(vehicleTemp,channel);
	}


	var playerIndex = me.getPlayerIndex(channel);
	var channelId = parseInt(channel);
	if(playerIndex == -1){
		//当前通道中没有需要添加的通道信息
		if(!isNaN(channelId) && channelId > 0){
			var temp = [];
			temp[0] = channelId;
			temp[1] = 0;
			me.channelsData.push(temp);//通道列表中添加数据
			// me.vehicleChannelStr =me.vehicleChannelStr + "," + channelId;//通道数据保存字符串中添加信息,暂不添加，author：shinioin，date：2019-2-19
	
			playerIndex = me.getPlayerIndex(channelId) + 1;
			if(me.shplayersTemp != null && !isNaN(playerIndex) && playerIndex > 0){
				var channelMsg = me.plateNo + "  通道" + channelId;
				me.shplayersTemp.showOnePlayerChannelMsg(playerIndex,channelMsg,true);
			}
		}
	}
	//发送开始视频命令
	if(!isNaN(channelId)){
		me.sendRealVideoRequest(channelId);
	}


	me.showDebugInfo(me.channelsData);
	
}



//对象被关闭前调用方法
RealTimeVideoPlayers.beforeClosed = function(){
	this.showDebugInfo("对象将要被关闭");
	// this.getChannelsDataChange();
}

//获取通道数据是否改变，如果改变，询问保存
RealTimeVideoPlayers.getChannelsDataChange = function(){
	this.showDebugInfo("数据库中通道数据信息 ： "+this.dbVehicleChannelStr + " , 本地通道数据信息 : "+this.vehicleChannelStr);

	if(this.vehicleChannelStr != this.dbVehicleChannelStr){
		if(confirm("车辆默认播放通道改变，是否更新保存到数据库?")){
			console.log("确认修改");
			this.saveChannelsDataStr();//保存数据
			this.dbVehicleChannelStr = this.vehicleChannelStr;
		}else{
			console.log("放弃修改");
		}
	}
}

//获取车辆通道数据保存字符串
RealTimeVideoPlayers.getChannelsDataStr = function(){
	return this.vehicleChannelStr;
}

//保存通道数据到数据库
RealTimeVideoPlayers.saveChannelsDataStr = function(){
	var me = this;
	me.showDebugInfo("保存数据库中的通道数据字符串..............");
	var updateFlg = "";

	var simNoTemp = me.simNo;
	simNoTemp = simNoTemp.substring(1,simNoTemp.length);
	var param = {
		vehicleId:me.vehicleId,
		simNo:simNoTemp,
		channelsDataStr:me.vehicleChannelStr,
	};
	var url = "";
	if(me.dbVehicleChannelStr == "") {//数据库不存在当前数据
		url = me.webPath + "/vehicleRealTimeVideo/insertChannelDataStr.action";
		updateFlg = "插入 ..";
	}else{
		url = me.webPath + "/vehicleRealTimeVideo/updateChannelDataStr.action";
		updateFlg = "更新 ..";
	}
	
	this.showDebugInfo("更新请求数据地址为 ："+ url + " , 参数为 ： ");
	me.showDebugInfo(param);

	Utility.getAjaxData(url,JSON.stringify(param),function(data){
		me.showDebugInfo(updateFlg + "通道数据请求返回的结果  ： ");
		me.showDebugInfo(data);
		if(data.success){
			me.showDebugInfo( updateFlg + "通道数据成功!");
		}
	});
}

//全屏显示
RealTimeVideoPlayers.requestFullScreen = function(element) {
	console.log("全屏方法启动");
	var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
	if (requestMethod) {
		requestMethod.call(element);
	} else if (typeof window.ActiveXObject !== "undefined") {
		var wscript = new ActiveXObject("WScript.Shell");
		if (wscript !== null) {
			wscript.SendKeys("{F11}");
		}
	}
	if(this.shplayersTemp != null){
		this.shplayersTemp.initPlayers(false);//不修改通道信息初始化
	}
}



/////////////////////////////////////////////////
/////////////////////////////////////////////////公共方法：输出调试、错误信息

//判断当前值是否为undefined
RealTimeVideoPlayers.isUndefined = function(value){
	try {
		if (value == 'undefined' || value == undefined || value == null) {
			return true;
		}
	} catch(event) {
		this.showErrorInfo(event);
	}
	return false;
}

//输出调试信息
RealTimeVideoPlayers.showDebugInfo = function(msg){
	if(this.debug){
		if(typeof(msg) != "string"){
			console.log("videoManagerNew debug --->");
			console.log(msg);
		}else{
			console.log("videoManagerNew debug --->"+msg);
		}
		
	}
}

//输出错误信息
RealTimeVideoPlayers.showErrorInfo = function(error){
	console.error("videoManagerNew error --->"+error);
}









/*
设置所有画面的声音大小
	该方法为页面中声音调整控件调用方法
*/
function onSliderValueChange (newV, oldV){
	// showDebugInfo(oldV + " , "+ newV);  
	var value = newV/10;
	RealTimeVideoPlayers.showDebugInfo("声音大小："+value);
	if(RealTimeVideoPlayers.shplayersTemp != null){
		RealTimeVideoPlayers.shplayersTemp.setAllVolume(value);
	}
}



/*

测试代码

*/






RealTimeVideoPlayers.name = "testName";


RealTimeVideoPlayers.test = function(msg){
	console.log("当前值："+this.name);
	this.name = msg;
	console.log("最新值："+this.name);
};

